﻿using AiXiu.Common;
using AiXiu.IDAL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.DAL
{
    public class UersServer : IUersServer
    {
        /// <summary>
        /// 做注册 --两个表做添加 提交到数据库
        /// </summary>
        /// <param name="tBLogins"></param>
        /// <returns></returns>
        public bool AddUser(TBLogins tBLogins)
        {
            AiXiuDbConten db = new AiXiuDbConten();
            db.TBLogins.Add(tBLogins);
            int tbils = db.SaveChanges();
            if (tbils > 0)
            {
                TBUsers tBUsers = new TBUsers()
                {
                    Id = tBLogins.Id,
                    CreationTime = DateTime.Now,
                    NickName = tBLogins.UserName,
                };
                db.TBUsers.Add(tBUsers);
                return db.SaveChanges() > 0;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 使用EntityState更新数据库
        /// </summary>
        /// <returns></returns>
        public TBUsers EditAvatar(TBUsers profile)
        {
            AiXiuDbConten dbConten = new AiXiuDbConten();
            TBUsers userEntity = dbConten.TBUsers.SingleOrDefault(e => e.Id == profile.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.Avatar = profile.Avatar;
            //更新到数据库
            dbConten.Entry(userEntity).State = EntityState.Modified;
            dbConten.SaveChanges();
            return userEntity;
        }

        /// <summary>
        /// 使用EntityState更新数据库
        /// </summary>
        /// <param name="profile"></param>
        /// <returns></returns>
        public TBUsers EditWithoutAvatar(TBUsers profile)
        {
            // 查询数据库
            AiXiuDbConten dbContext = new AiXiuDbConten();
            TBUsers userEntity = dbContext.TBUsers.SingleOrDefault(m => m.Id == profile.Id);
            if (userEntity == null)
                return null;
            userEntity.NickName = profile.NickName;
            userEntity.Sex = profile.Sex;
            userEntity.Birthday = profile.Birthday;
            userEntity.Address = profile.Address;
            userEntity.Hobby = profile.Hobby;
            // 更新到数据库
            dbContext.Entry(userEntity).State = EntityState.Modified;
            dbContext.SaveChanges();
            // 返回结果
            return userEntity;

        }

        /// <summary>
        /// 找到Id找信息
        /// </summary>
        /// <param name="Id">编号</param>
        /// <returns></returns>
        public TBUsers FindByUserId(int Id)
        {
            AiXiuDbConten dbContext = new AiXiuDbConten();
            //return dbContext.TBUsers.Find(userId);
            return dbContext.TBUsers.SingleOrDefault(e => e.Id == Id);
        }

        /// <summary>
        /// 手机号 查重
        /// </summary>
        /// <param name="mobile">手机号</param>
        /// <returns></returns>
        public bool IsSomeMobile(string mobile)
        {
            AiXiuDbConten db = new AiXiuDbConten();
            var model = db.TBLogins.SingleOrDefault(e => e.MobileNumber == mobile);
            if (model != null)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 用户名查重
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public bool IsSomeUersName(string userName)
        {
            AiXiuDbConten db = new AiXiuDbConten();
            var model = db.TBLogins.SingleOrDefault(e => e.MobileNumber == userName);
            if (model != null)
            {
                return true;
            }
            return false;
        }


    }
}
